package top.maof.book.rank.book;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.maof.book.model.History;
import top.maof.book.rank.Handle;
import top.maof.book.rank.HandleChain;
import top.maof.book.service.BookInfoService;
import top.maof.book.service.HistoryService;
import top.maof.book.type.HotType;

@Component
public class ClickBookHandleRank implements Handle<BookHo> {

    @Autowired
    private HistoryService historyService;

    @Autowired
    BookInfoService bookInfoService;

    @Override
    public void add(BookHo bookHo, HandleChain handleChain) {
        if (bookHo.getHotType() == HotType.CLICK) {
            bookInfoService.addClick(bookHo.getId(), 1);
            History history = new History();
            history.setBookId(bookHo.getId());
            history.setUserId(bookHo.getUserId());
            history.setIp(bookHo.getIp());
            historyService.save(history);
        }

        handleChain.add(bookHo, handleChain);
    }

    @Override
    public void sub(BookHo bookHo, HandleChain handleChain) {
        handleChain.sub(bookHo, handleChain);
    }
}
